package org.destinyshine.test.servlet;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * Created by destinyliu on 2016/2/27.
 */
public class HelloServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String content = IOUtils.toString(reader);
        String name = request.getParameter("name");
        String[] names = request.getParameterValues("name");
        response.getWriter().println("<html>" +
                "<body>" +
                "<p style='color:green'>hello, post, content=" + content + "</p>" +
                "<p style='color:green'>hello, post, name=" + name + "</p>" +
                "<p style='color:green'>hello, post, names=" + (names == null?"":String.join(",", names) )+ "</p>" +
                "</body>" +
                "</html>");
        response.flushBuffer();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String[] names = request.getParameterValues("name");
        response.getWriter().println("<html>" +
                "<body>" +
                "<p style='color:green'>hello, get, name=" + name + "</p>" +
                "<p style='color:green'>hello, get, names=" + String.join(",", names) + "</p>" +
                "</body>" +
                "</html>");
        response.flushBuffer();
    }

}
